package com.feiyisys.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.feiyisys.common.PageResult;
import com.feiyisys.common.Result;
import com.feiyisys.dto.FavoriteActivityDTO;
import com.feiyisys.dto.FavoriteActivityResponseDTO;
import com.feiyisys.service.FavoriteActivityService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户收藏活动记录表 前端控制器
 * </p>
 *
 * @author wq
 * @since 2025-10-10
 */
@RestController
@RequestMapping("/favoriteActivity")
@CrossOrigin("*")
public class FavoriteActivityController {

    @Resource
    private FavoriteActivityService favoriteActivityService;

    /**
     * 添加收藏
     */
    @PostMapping("/add")
    public Result<Boolean> addFavorite(@RequestBody FavoriteActivityDTO favoriteActivityDTO) {
        try {
            boolean result = favoriteActivityService.addFavorite(favoriteActivityDTO.getUserId(), favoriteActivityDTO.getActivityId());
            return Result.success(result);
        } catch (IllegalArgumentException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.fail("收藏失败，请稍后重试");
        }
    }

    /**
     * 取消收藏
     */
    @DeleteMapping("/remove")
    public Result<Boolean> removeFavorite(@RequestParam Long userId, @RequestParam Long activityId) {
        boolean removed = favoriteActivityService.removeFavorite(userId, activityId);
        if (removed) {
            return Result.success(true);
        }
        return Result.error("收藏记录不存在");
    }

    /**
     * 判断是否已收藏
     */
    @GetMapping("/check")
    public Result<Boolean> checkFavorite(@RequestParam Long userId, @RequestParam Long activityId) {
        boolean result = favoriteActivityService.isFavorite(userId, activityId);
        return Result.success(result);
    }

    /**
     * 获取用户收藏列表
     */
    @GetMapping("/user/{userId}")
    public PageResult<FavoriteActivityResponseDTO> getUserFavorites(
            @PathVariable Long userId,
            @RequestParam(defaultValue = "1") Long current,
            @RequestParam(defaultValue = "10") Long size) {
        try {
            IPage<FavoriteActivityResponseDTO> page = favoriteActivityService.getFavoriteActivities(userId, current, size);
            return PageResult.success(page);
        } catch (Exception e) {
            return PageResult.error("获取收藏列表失败");
        }
    }
}
